package com.zrf.testchatgpt.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrf.testchatgpt.entity.Detail;
import com.zrf.testchatgpt.entity.UserPainting;
import com.zrf.testchatgpt.entity.vo.DetailVo;
import com.zrf.testchatgpt.entity.vo.PaintingVo;
import com.zrf.testchatgpt.mapper.UserPaintingMapper;
import com.zrf.testchatgpt.util.MinioUtil;
import com.zrf.testchatgpt.util.Result;
import com.zrf.testchatgpt.util.TranslateUtil;
import com.zrf.testchatgpt.util.pythonServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequiresRoles(value = {"user","admin"},logical= Logical.OR)
public class AIHUIHUAController {
    @Autowired
    TranslateUtil translateUtil;
    @Autowired
    MinioUtil minioUtil;
    @Autowired
    UserPaintingMapper mapper;
    @Autowired
    pythonServiceUtil pythonServiceUtil;

    @RequestMapping("getAIHUIHUA")
    public Result getAIHUIHUA(@RequestParam Map<String,Object> map){
        Object steps=map.get("steps");
        Object cfg_scale=map.get("cfg_scale");
        Integer userId = Integer.parseInt(map.get("userId").toString()) ;
        Map<String, Object> english = translateUtil.toTranslate(map);

        Map<String,Object> result=new HashMap<>();
        result.put("prompt",english.get("prompt"));
        result.put("steps",steps);
        result.put("cfg_scale",cfg_scale);
        JSONObject data = pythonServiceUtil.doPostTestThree(result);

        if (data.containsKey("code")){
            return new Result("500",data.getString("msg"), null);
        }else {
            String filename = minioUtil.MinioUploadBase64(data);
            UserPainting userPainting=
                    UserPainting.
                            builder().
                            userId(userId).
                            paintingUrl(filename).
                            isDelete("1").
                            build();
            try{
                mapper.insert(userPainting);
            }catch (Exception e){
                throw new RuntimeException("插入错了");
            }
            return new Result("200","成功",filename);
        }
    }

    @RequestMapping("changeModel")
    @RequiresPermissions("change_Model")
    public Result changeModel(@RequestParam Map<String,Object> map){
        if(map==null){
            return new Result("500","参数错误",null);
        }
        JSONObject jsonObject = pythonServiceUtil.stableChangeModel(map);
        return new Result("200","修改成功",null);
    }

    @PostMapping("insertPainting")
    public Result insertPainting(@RequestBody UserPainting detail){
        if (detail==null){
            return Result.builder().code("500").msg("参数为空").data(null).build();
        }
        detail.setIsDelete("1");
        int i = mapper.insertDetail(detail);
        if (i>0){
            return Result.builder().code("200").msg("成功").data(detail.getId()).build();
        }else {
            return Result.builder().code("500").msg("插入失败").data(null).build();
        }
    }
    @PostMapping("updatePainting")
    public Result updatePainting(@RequestBody UserPainting detail){
        if (detail==null){
            return Result.builder().code("500").msg("参数为空").data(null).build();
        }
        LambdaQueryWrapper<UserPainting> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserPainting::getId,detail.getId());
        int i = mapper.update(detail,wrapper);
        if (i>0){
            return Result.builder().code("200").msg("成功").data(null).build();
        }else {
            return Result.builder().code("500").msg("插入失败").data(null).build();
        }
    }
    @GetMapping("getPaintingByPage")
    public Result getPaintingByPage(String username,Integer current,Integer size,String isdelete){
        LambdaQueryWrapper<UserPainting> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(UserPainting::getUserId,username);
        wrapper.eq(UserPainting::getIsDelete,isdelete);
        IPage<UserPainting> page = new Page<>(current, size);
        mapper.selectPage(page, wrapper);
        PaintingVo userVo =  PaintingVo.builder().current(current).size(size).total(page.getTotal()).paintingList(page.getRecords()).
                build();
        if (page.getTotal()>0){
            return Result.builder().code("200").data(userVo).msg("查询成功!!").build();
        }
        return Result.builder().code("500").data(null).msg("查询为空!!").build();
    }
    @GetMapping("getModels")
    @RequiresPermissions("change_Model")
    public Result getModels(){
        JSONArray models = pythonServiceUtil.getModels();
        return Result.builder().code("200").msg("成功").data(models).build();
    }
    @GetMapping("getcurrentModel")
    @RequiresPermissions("change_Model")
    public Result getcurrentModel(){
        Object jsonObject = pythonServiceUtil.getcurrentModel();
        return Result.builder().code("200").msg("成功").data(jsonObject).build();
    }

}
